package com.jiuyuan.controller;

import com.jiuyuan.entity.Admin;
import com.jiuyuan.entity.vo.LoginVo;
import com.jiuyuan.entity.vo.RespBean;
import com.jiuyuan.entity.vo.RespBeanEnum;
import com.jiuyuan.service.IAdminService;
import com.jiuyuan.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @program: hrms
 * @description:
 * @author: Feng
 * @create: 2023-02-15 09:22
 **/

@RestController
public class LoginController {
    @Autowired
    ILoginService loginService;
    @Autowired
    IAdminService adminService;

    @PostMapping("/login")
    public RespBean login(@RequestBody LoginVo loginVo, HttpServletRequest req){
        //
        return loginService.login(loginVo.getUsername(),loginVo.getPassword(),loginVo.getCode(),req);
    }

    @GetMapping("/getUserInfo")
    public RespBean getUserInfo(Principal principal){
        if(null == principal)
            return RespBean.error(RespBeanEnum.NOT_LOGIN);
        Admin admin = adminService.getAdminByUsername(principal.getName());
        admin.setPassword(null);
        admin.setRoles(adminService.getRolesByAdminId(admin.getId()));
        return RespBean.success("获取成功",admin);
    }

    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("登出成功");
    }
}






















